package com.bobo.swing;

import java.awt.BorderLayout;

import javax.swing.JFrame;
import javax.swing.JProgressBar;
import javax.swing.WindowConstants;

/**
 * Swing 实现进度条变化
 * @author win7
 *
 */
public class JoinTest extends JFrame{

	//定义两个线程
	private Thread ta;
	private Thread tb;
	//定义两个进度条组件
	final JProgressBar jProgressBar1=new JProgressBar();
	final JProgressBar jProgressBar2=new JProgressBar();
	
	public JoinTest(){
		//将进度条设置在窗体的最北边
		getContentPane().add(jProgressBar1, BorderLayout.NORTH);
		//将进度条设置在窗体的最南边
		getContentPane().add(jProgressBar2, BorderLayout.SOUTH);
		
		//设置进度条显示数字字符
		jProgressBar1.setStringPainted(true);
		jProgressBar2.setStringPainted(true);
		
		//使用匿名内部类实例化Thread实例
		ta=new Thread(new Runnable() {
			int count=0;
			public void run() {
				while(true){
					jProgressBar1.setValue(count+=2);//设置进度条当前值
					try {
						Thread.sleep(100);
						tb.join();
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					if(count==100){
						break; //跳出循环，终止线程
					}
				}
			}
		});
		ta.start();//启动线程
		
		//使用匿名内部类实例化Thread实例
		tb=new Thread(new Runnable() {
			int count2=0;
			public void run() {
				while(true){
					jProgressBar2.setValue(++count2);//设置进度条当前值
					try {
						Thread.sleep(100);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					if(count2==100){
						break;
					}
				}
			}
		});
		tb.start();//启动线程
	}
	
	//设置窗体的各种属性的方法
	public static void init(JFrame jf,int x,int y){
		 jf.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		 jf.setSize(x, y);
		 jf.setVisible(true);
	}
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		init(new JoinTest(),100,100);
	}

}
